﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Linq;
using Kunlun.Models;
using System.IO;
using Kunlun.BusinessLogic;
namespace Kunlun.Controllers
{
    public class FlexController : Controller
    {
        //
        // GET: /Flex/
        KunLunEntities entities = new KunLunEntities();
        Kunlunphotolib lib = new Kunlunphotolib();
        FlexSave flex = new FlexSave();
        //[Authorize]
        public ActionResult Index(int? id)
        {
            int userid = 8;// Convert.ToInt32(Request.Cookies.Get("userId").Value);
			
            if (id == null)
            {
                
                string indoc = lib.FlexXd(userid);
                return Content(indoc, "text/xml", System.Text.Encoding.UTF8);
            }
            else
            {
                int libid = Convert.ToInt32(id);
                return Content(lib.FlexPhoto(userid, libid), "text/xml",System.Text.Encoding.UTF8);
            }
        }
        /// <summary>
        /// 保存用户作品
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            try
            {


                int userid = 8;  //Convert.ToInt32(Request.Cookies.Get("userId").Value);
                string id = collection["id"].ToString();
                string content = collection["content"].ToString();
                string file = collection["file"].ToString();
                string workid = collection["workid"].ToString();
                string worksname = collection["worksname"].ToString();
                string pagecount = collection["pagecount"].ToString();
                return Content(flex.SaveXml(content, id, userid.ToString(), file, workid, pagecount, worksname).ToString());
                

            }
            catch(Exception ex)
            {

                WebLog.ErrorLog(ex);
                return Content("false");
            }
           
        }
        /// <summary>
        /// 用户作品表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetWorks()
        {
            int userid = 8;
            List<Works> works = (entities.Works.Where(w => w.UserID == userid)).ToList();
            System.Text.StringBuilder app = new System.Text.StringBuilder();
            app.Append("<works>");
            foreach (Works w in works)
            {
                app.Append(String.Format("<work id=\"{0}\" worksname=\"{1}\" Thumbnail=\"{2}{3}\" xmlpath=\"{2}{4}\" tempid=\"{5}\" pid=\"{6}\" pagescount=\"{7}\" />", w.ID, w.WorksName, WebLog.WorksView, w.Thumbnail, w.XMLPath, w.TempID, w.ProductID, w.PagesCount));
            }
            app.Append("</works>");
            return Content(app.ToString(), "text/xml", System.Text.Encoding.UTF8);
        }
        /// <summary>
        /// 获取同类型模板
        /// </summary>
        /// <param name="tempid"></param>
        /// <returns></returns>
        public ActionResult GetTempList(string id)
        {
           List<Templete> temp=  KunlunTemplete.GetTemplete(int.Parse(id));
           System.Text.StringBuilder app = new System.Text.StringBuilder();
           app.Append("<temps>");
           foreach (Templete t in temp)
           {
               app.Append(String.Format("<temp id=\"{0}\" tempname=\"{1}\" Thumbnail=\"{2}{3}\" TempletePath=\"{2}{4}\"  pid=\"{5}\"  />", t.Tid, t.TempleteName, WebLog.TempsView, t.Thumnail, t.TempletePath, t.ProductID));
           }
           app.Append("</temps>");
           return Content(app.ToString(), "text/xml", System.Text.Encoding.UTF8);
        }
    }
}
